package com.example.hao.flowercityfm.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.hao.flowercityfm.Fragment.DiscoverFragment;
import com.example.hao.flowercityfm.Fragment.HomeFragment;
import com.example.hao.flowercityfm.Fragment.LiveFragment;
import com.example.hao.flowercityfm.Fragment.MineFragment;
import com.example.hao.flowercityfm.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.tl)
    TabLayout tl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initToolbar();
        initLLContainer();
        showFragment(0);
    }


    private void initLLContainer() {

    }

    private void initToolbar() {
        tl.addTab(tl.newTab().setText("首页").setTag(0));
        tl.addTab(tl.newTab().setText("直播").setTag(1));
        tl.addTab(tl.newTab().setText("发现").setTag(2));
        tl.addTab(tl.newTab().setText("我的").setTag(3));
        tl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int tag = (int) tab.getTag();
                showFragment(tag);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    int currentIndex = -1;
    private Fragment[] fragments = new Fragment[4];

    public void showFragment(int nextIndex) {

        if (currentIndex == nextIndex) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragments[nextIndex] == null) {
            switch (nextIndex) {
                case 0:
                    fragments[nextIndex] = new HomeFragment();
                    break;
                case 1:
                    fragments[nextIndex] = new LiveFragment();
                    break;
                case 2:
                    fragments[nextIndex] = new DiscoverFragment();
                    break;
                case 3:
                    fragments[nextIndex] = new MineFragment();
                    break;
            }
            ft.add(R.id.fl_container, fragments[nextIndex]);
        } else {
            ft.show(fragments[nextIndex]);
        }
        if (currentIndex != -1) {
            ft.hide(fragments[currentIndex]);
        }
        ft.commit();
        currentIndex = nextIndex;
    }
}
